<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keywords" placeholder="角色名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="handleSelect">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表-->
    <el-table :data="dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="角色名">
      </el-table-column>
      <el-table-column prop="sn" label="编码描述">
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        :total="total"
        style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑或修改界面-->
    <el-dialog :title="title" :visible.sync="formVisible" :close-on-click-modal="false">
      <el-form :model="form" label-width="80px" :rules="formRules" ref="form">
        <el-form-item label="角色名" prop="name">
          <el-input v-model="form.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="编码描述">
          <el-input v-model="form.sn"></el-input>
        </el-form-item>
        <el-form-item label="角色权限">
          <el-tree :data="permissionTree"
                   :props="defaultProps"
                   show-checkbox
                   accordion
                   node-key="id"
                   ref="tree"
                   style="border: #d7d7d9 1px solid; border-radius: 5px"></el-tree>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="formVisible = false">取消</el-button>
        <el-button type="primary" @click.native="formSubmit" :loading="formLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from "../../common/js/util";
import {batchRemoveUser, editUser} from "../../api/api";

export default {
  name: "Role",

  data(){
    return{
      query:{
        keywords: '',
      },
      dataList: [], //列表数据总数
      listLoading: false, //加载
      sels: [], //选择多项
      total: 0, //列表数据总条数
      currentPage: 1, //当前默认页面
      pageSize: 10, //默认页面条数
      title: "", //表单名
      formVisible: false, //默认关闭弹出框
      //编辑或修改界面数据
      form: {
        id: null,
        name: null,
        sn: null
      },
      //表单验证
      formRules: {
        name: [
          { required: true, message: '请输入角色名', trigger: 'blur' }
        ]
      },
      formLoading: false, //不显示加载
      permissionTree: [], //权限树
      //权限树下的字段设置
      defaultProps: {
        children: 'children',
        label: 'name'
      },


    }
  },

  methods: {
    //查询
    handleSelect(){
      this.currentPage = 1
      this.listLoadingData()
    },

    //增加
    handleAdd(){
      //更改表单名
      this.title = "新增"
      //1.清空表单数据
      this.form = {
        id: null,
        name: null,
        sn: null
      }
      //2.打开弹出框
      this.formVisible = true

      //清空树形菜单
      this.$refs.tree.setCheckedKeys([]);
    },

    //选择项发生改变时触发
    selsChange(val){
      this.sels = val
    },

    //根据权限id查询角色id
    loadPermissionIdByRoleId(val){
      this.$http.get("/role/permission/" + val).then(res => {
        this.$nextTick(() => {
          //查询当前角色拥有的数据权限
          this.$refs.tree.setCheckedKeys(res.data.data);
        })
      })
    },

    //编辑数据
    handleEdit(index,row){
      //更改表单名
      this.title = "编辑"
      //1.数据回显
      Object.assign(this.form,row)
      //2.打开弹出框
      this.formVisible = true

      //回显树形菜单
      this.loadPermissionIdByRoleId(row.id)
    },

    //删除数据
    handleDel(index,row){
      //通过表单验证
      this.$confirm('确认删除数据吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.$http.delete("/role/" + row.id).then((res) => {
          this.formLoading = false;
          if (res.data.success){
            this.$message({
              message: '删除成功',
              type: 'success'
            });
          }
          else {
            this.$message.error(res.data.message)
          }
          this.listLoadingData()
        });
      });
    },

    //加载列表数据
    listLoadingData(){
      //加载列表
      this.listLoading = true;
      //封装数据
      let query = {
        currentPage: this.currentPage, //当前页面
        pageSize: this.pageSize, //页面条数
        keywords: this.query.keywords, //关键字查询
      }
      //发送请求获取列表数据
      this.$http.post('/role',query).then(res=>{
        //数据总条数
        this.total = res.data.data.count
        //列表数据
        this.dataList = res.data.data.data
        //关闭加载列表
        this.listLoading = false;
      })
    },

    //批量删除
    batchRemove(){
      var ids = this.sels.map(item => item.id)
      this.$confirm('确认删除选中数据吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //NProgress.start();
        // let para = { ids: ids };
        this.$http.patch("/role",ids).then((res) => {
          this.listLoading = false;
          //NProgress.done();
          this.$message({
            message: '删除成功',
            type: 'success'
          });
          this.listLoadingData();
        });
      }).catch(() => {

      });
    },

    //当前页条数
    handleSizeChange(val){
      this.pageSize = val
      this.listLoadingData()
    },

    //指向当前页
    handleCurrentChange(val){
      this.currentPage = val
      this.listLoadingData()
    },

    //提交弹出框
    formSubmit(){
      this.$refs.form.validate((valid) => {
        if (valid) {
          //通过表单验证
          this.$confirm('确认提交数据吗？', '提示', {}).then(() => {
            //用户点击了确认
            this.formLoading = true;
            //组装提交的数据
            let params = {
              role: this.form,
              permissionIds: this.$refs.tree.getCheckedKeys(),
            }

            this.$http.put("/role/addOrUpdate",params).then((res) => {
              this.formLoading = false;
              if (res.data.success){
                this.$message({
                  message: '提交成功',
                  type: 'success'
                });
              }
              else {
                this.$message.error(res.data.message)
              }
              this.formVisible = false
              this.formLoading = false
              this.listLoadingData()
            });
          });
        }
      });
    },

    //加载权限树
    loadPermissionTree(){
      this.$http.get("/permission/tree").then(res =>{
        this.permissionTree = res.data.data
      })
    }

},

  mounted() {
    this.listLoadingData()
    this.loadPermissionTree()
  }
}
</script>

<style scoped></style>